home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / misc / trunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  5.2 KB  |  231 lines

  1. mailx Revision: 64.64    Date: 89/07/24 09:21:08    Type ? for help.
  2. "/usr/mail/dc1ik": 1 message 1 new
  3. >N  1 dk5sg!deyke        Fri May 31 18:09  224/5052  Re: trunc.c?
  4. mailx> "/users/funk/dc1ik/mbox" [Appended] 224/5052
  5. mailx> Message  1:
  6. From dk5sg!deyke Fri May 31 18:09 MES 1991
  7. Received: from dk5sg by db0sao; Fri, 31 May 91 18:09:43 mes
  8. Return-Path: <dk5sg!deyke>
  9. Received: by mdddhd.HP.COM; Fri, 31 May 91 10:09:13 mdt
  10. From: Dieter Deyke <mdddhd!deyke>
  11. Full-Name: Dieter Deyke
  12. Message-Id: <9105311609.AA19464@mdddhd.HP.COM>
  13. Subject: Re: trunc.c?
  14. To: dc1ik (Olaf Erb)
  15. Date: Fri, 31 May 91 10:09:11 MDT
  16. In-Reply-To: <m0jjDgo-0001XeC@rnieph.rni.sub.org>; from "Olaf Erb" at May 31, 91 5:55 pm
  17. Reply-To: deyke@hpfcmdd.fc.hp.com
  18. Mailer: Elm [revision: 64.9]
  19. Status: R
  20.  
  21. > Hallo Dieter,
  22. > habe bei Dir irgendwo trunc.c,s o. ae. gesehen, hast Du den Src davon?
  23. > 73s
  24. > Olaf
  25.  
  26. #---------------------------------- cut here ----------------------------------
  27. # This is a shell archive.  Remove anything before this line,
  28. # then unpack it by saving it in a file and typing "sh file".
  29. #
  30. # Wrapped by Dieter Deyke <deyke@mdddhd> on Fri May 31 10:08:16 1991
  31. #
  32. # This archive contains:
  33. #       trunc.c 
  34. #
  35. # Error checking via wc(1) will be performed.
  36. # Error checking via sum(1) will be performed.
  37.  
  38. LANG=""; export LANG
  39. PATH=/bin:/usr/bin:$PATH; export PATH
  40.  
  41. if sum -r </dev/null >/dev/null 2>&1
  42. then
  43.         sumopt='-r'
  44. else
  45.         sumopt=''
  46. fi
  47.  
  48. echo x - trunc.c
  49. cat >trunc.c <<'@EOF'
  50. /* trunc - truncate files
  51.  
  52.    trunc [ options ] file ...
  53.  
  54.    -m USER       mail file to USER before truncating or removing
  55.                  (done only if the file is not empty).
  56.  
  57.    -b BYTES      keep only the last BYTES bytes of the file.
  58.  
  59.    -r            remove file (useful with -m option).
  60.  
  61.    -o OWNER      change owner of file to OWNER (must be numeric).
  62.  
  63.    -g GROUP      change group of file to GROUP (must be numeric).
  64.  
  65.    -c MODE       change mode of file to MODE.
  66.  
  67. */
  68.  
  69. static char  sccsid[] = "@(#) trunc.c   1.2   87/03/10 21:32:16";
  70.  
  71. #include <fcntl.h>
  72. #include <stdio.h>
  73. #include <sys/types.h>
  74. #include <sys/stat.h>
  75.  
  76. #define BUFSIZE   8*1024
  77. #define MAXINT    0x7fffffff
  78.  
  79. /*---------------------------------------------------------------------------*/
  80.  
  81. void mail_file_to_user(filename, user)
  82. char  *filename, *user;
  83. {
  84.  
  85.   FILE * fpin, *fpout;
  86.   char  cmdline[256];
  87.   int  c;
  88.  
  89.   if (!(fpin = fopen(filename, "r"))) return;
  90.   sprintf(cmdline, "/bin/mail %s", user);
  91.   if (!(fpout = popen(cmdline, "w"))) return;
  92.   fprintf(fpout, "To: %s\nSubject: %s\n\n", user, filename);
  93.   while ((c = getc(fpin)) != EOF) putc(c, fpout);
  94.   fclose(fpin);
  95.   pclose(fpout);
  96. }
  97.  
  98. /*---------------------------------------------------------------------------*/
  99.  
  100. void truncate_file(filename, bytes)
  101. char  *filename;
  102. int  bytes;
  103. {
  104.  
  105.   extern long  lseek();
  106.  
  107.   char  buffer[BUFSIZE];
  108.   char  tempfile[256];
  109.   int  fi;
  110.   int  fo;
  111.   unsigned  i;
  112.  
  113.   sprintf(tempfile, "/tmp/trunc%d", getpid());
  114.  
  115.   fi = open(filename, O_RDONLY);
  116.   fo = creat(tempfile, 0600);
  117.   lseek(fi, (long) -bytes, 2);
  118.   while ((i = read(fi, buffer, BUFSIZE)) > 0)
  119.     write(fo, buffer, i);
  120.   close(fi);
  121.   close(fo);
  122.  
  123.   fi = open(tempfile, O_RDONLY);
  124.   fo = creat(filename, 0666);
  125.   while ((i = read(fi, buffer, BUFSIZE)) > 0)
  126.     write(fo, buffer, i);
  127.   close(fi);
  128.   close(fo);
  129.  
  130.   unlink(tempfile);
  131. }
  132.  
  133. /*---------------------------------------------------------------------------*/
  134.  
  135. main(argc, argv)
  136. int  argc;
  137. char  **argv;
  138. {
  139.  
  140.   extern char  *optarg;
  141.   extern int  optind;
  142.   extern long  strtol();
  143.   extern void exit();
  144.  
  145.   char  *filename;
  146.   char  *user = 0;
  147.   int  bytes = MAXINT;
  148.   int  c;
  149.   int  errflag = 0;
  150.   int  group;
  151.   int  mode;
  152.   int  owner;
  153.   int  remove = 0;
  154.   int  set_group = 0;
  155.   int  set_mode = 0;
  156.   int  set_owner = 0;
  157.   struct stat statbuf;
  158.  
  159.   while ((c = getopt(argc, argv, "m:b:ro:g:c:")) != EOF)
  160.     switch (c) {
  161.     case 'm':
  162.       user = optarg;
  163.       break;
  164.     case 'b':
  165.       bytes = atoi(optarg);
  166.       break;
  167.     case 'r':
  168.       remove = 1;
  169.       break;
  170.     case 'o':
  171.       owner = atoi(optarg);
  172.       set_owner = 1;
  173.       break;
  174.     case 'g':
  175.       group = atoi(optarg);
  176.       set_group = 1;
  177.       break;
  178.     case 'c':
  179.       mode = strtol(optarg, 0, 8);
  180.       set_mode = 1;
  181.       break;
  182.     case '?':
  183.       errflag = 1;
  184.       break;
  185.     }
  186.  
  187.   if (errflag) {
  188.     fprintf(stderr, "usage: trunc [-m user] [-b bytes] [-r] [-o owner] [-g group] [-c mode] file ...\n");
  189.     return 2;
  190.   }
  191.  
  192.   for (; optind < argc; optind++) {
  193.     filename = argv[optind];
  194.  
  195.     if (stat(filename, &statbuf)) continue;
  196.     if (set_group && !set_owner) owner = statbuf.st_uid;
  197.     if (set_owner && !set_group) group = statbuf.st_gid;
  198.  
  199.     if (user && statbuf.st_size) mail_file_to_user(filename, user);
  200.  
  201.     if (remove) {
  202.       unlink(filename);
  203.       continue;
  204.     }
  205.     if (bytes <= 0)
  206.       close(creat(filename, 0644));
  207.     else if (bytes < statbuf.st_size)
  208.       truncate_file(filename, bytes);
  209.  
  210.     if (set_mode) chmod(filename, mode);
  211.     if (set_owner || set_group) chown(filename, owner, group);
  212.   }
  213.   exit(0);
  214.   return 0;
  215. }
  216. @EOF
  217. set `sum $sumopt <trunc.c`; if test $1 -ne 38042
  218. then
  219.         echo ERROR: trunc.c checksum is $1 should be 38042
  220. fi
  221. set `wc -lwc <trunc.c`
  222. if test $1$2$3 != 1664403578
  223. then
  224.         echo ERROR: wc results of trunc.c are $* should be 166 440 3578
  225. fi
  226.  
  227. chmod 644 trunc.c
  228.  
  229. exit 0
  230.  
  231. mailx>